﻿import os
from gtrans import translate
linguagens = ['pten','enpt']

def gossip_text(msg):
	data = "<gossip_text>" + translate("EN","PT",msg[13:]).encode("utf-8")
	return data +"\n"
	
def quest_progress(msg):
	data = "<quest_progress>" + translate("EN","PT",msg[16:]).encode("utf-8")
	return data +"\n"
	
def say(msg):
	for l in linguagens:
		if l in msg:
			data=l + translate(l[:2],l[2:4],msg.replace(l,"")).encode("utf-8")
			return data +"\n"
	return "pten nada\n" 
	
def traduz(msg):
	if "<gossip_text>" in msg:
		return gossip_text(msg)
	if "<quest_progress>" in msg:
		return quest_progress(msg)
		
	else: # <say>
		return say(msg)
	
	

	
	
def server():
	import socket

	HOST = '127.0.0.1'                 # Symbolic name meaning all available interfaces
	PORT = 50001              # Arbitrary non-privileged port
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind((HOST, PORT))
	s.listen(1)
	while 1:
		conn, addr = s.accept()
		print 'Connected by', addr
		while 1:
			data = conn.recv(4048)
			print ("'%s'"%data)
			if not data:break
			if "<quit>" in data:
				return
			conn.send(traduz(data))
		conn.close()
def get_wow_path():
    from _winreg import *
    key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Blizzard Entertainment\World of Warcraft', 0, KEY_ALL_ACCESS)
    return QueryValueEx(key,"InstallPath")[0]

def rodando_processo(processo):
    cmd = os.popen('query process')
    x = cmd.readlines()
    for y in x:
        p = y.find(processo)
        if p >= 0:
            return True

    return False

def instala_addon(raiz):
	import shutil
	instalacao_path=os.path.normpath("%s\\Interface\\Addons\\dicionario" % get_wow_path())
	if not os.path.exists(instalacao_path):
		shutil.copytree("%s\\dicionario"%raiz,instalacao_path)
	else:
		print("ja instalado")
	
def start_wow():
    os.startfile("%swow.exe" %get_wow_path())
    

if __name__=="__main__":
	raiz = os.path.normpath("%s\\wowbabel" % os.environ["ProgramFiles"] )
	"""instala_addon(raiz)
	if not rodando_processo("wow.exe"):
		start_wow()
	if not rodando_processo("wowrogue.exe"):
		os.startfile(os.path.normpath(os.path.abspath(raiz)+"\\wowrogue.exe"))"""
	server()

